import { saveAs } from 'file-saver';

class AlgorithmBase{
  constructor(id='AlgorithmBase')
  {
    this.id          = id;
    this.name        = '';
    this.description = '';
    // 图/表，训练和测试DOM对象
    this.domFigure = undefined;
    this.domTable  = undefined;
    this.domTrain  = undefined;
    this.domModel  = undefined;
    this.domTest   = undefined;

    // 训练集和测试集
    this.trainDSList = [];
    this.testDSList  = [];
    this.trainDS   = {};
    this.testDS    = {};
    // 训练模型
    this.model     = {};
  }
  // 初始化训练集和测试集，和模型，必要时给定初值
  init(){ 
    console.log(this.id);
    this.clear();
  }
  // 清除DOM数据
  clear(){
    if(this.domFigure) this.domFigure.innerHTML = '';
    if(this.domTable)  this.domTable.innerHTML  = '';
    if(this.domTrain)  this.domTrain.innerHTML  = '';
    if(this.domModel)  this.domModel.innerHTML  = '';
    if(this.domTest)   this.domTest.innerHTML   = '';
  }
  // 加载训练集
  loadTrain(){ }
  // 保存训练集
  saveTrain(){}
  // 随机生成训练集
  createRandomTrain(){ }
  // 显示训练集
  displayTrain(){ if(this.domTrain) this.domTrain.innerHTML = ''; }
  // 设置训练集
  setTrainDS(index){
    if(index < 0 || index >= this.trainDSList.length) return ;
    this.trainDS = this.trainDSList[index];
    this.print();
  }
  // 加载测试集
  loadTest(){ }
  // 保存测试集
  saveTest(){ }
  // 随机测试集
  createRandomTest(){ }
  // 显示测试集
  displayTest(){ if(this.domTest) this.domTest.innerHTML='';}
  // 设置测试集
  setTestDS(index){
    if(index < 0 || index >= this.testDSList.length) return ;
    this.testDS = this.testDSList[index];
    this.print();
  }
  // 训练
  train(){ }
  // 导入模型
  loadModel(){ }
  // 保存模型
  saveModel(){ }
  // 显示模型
  displayModel(){ if(this.domModel) this.domModel.innerHTML=''; }
  // 测试
  test(){ }
  // 显示
  print(){
    this.displayTrain();
    this.displayModel();
    this.displayTest();
  }
  // 载入
  load(){ }

  // 保存
  save(){
    let json = {};
    json.TrainDS = JSON.stringify(this.trainDS);
    json.Model   = JSON.stringify(this.model);
    json.TestDS  = JSON.stringify(this.testDS);

    let blob = new Blob([JSON.stringify(json)], {type: "text/plain;charset=utf-8"});
    saveAs(blob, `${this.id}.json`);
  }
}

export default AlgorithmBase;